package org.teammach.laddercomp.dao;

import java.util.HashMap;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.teammach.laddercomp.shared.Comment;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public enum CommentManager {

	INSTANCE;
	
	public List<Comment> getComments(int start, int end) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Comment.class);
		query.setOrdering("created desc");
		query.setRange("" + start + "," + end);
		query.setFilter("deleted == false");
		List<Comment> entries = (List<Comment>) query.execute();
		entries.size();
		pm.close();
		return entries;
	}
	
	
	public void addComment(String content) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if(user != null) {
			Comment comment = new Comment(content, user);
			pm.makePersistent(comment);
		}
		pm.close();
	}
	

	
	public void deleteComment(String key){
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Comment comment = (Comment)pm.getObjectById(Comment.class, KeyFactory.stringToKey(key));
		comment.setDeleted(true);
		pm.makePersistent(comment);
		pm.close();
	}
	
	public static String getUserNameForEmail(String email) {
		HashMap<String, String> userNames = new HashMap<String, String>();
		userNames.put("dscerri@gmail.com", "Dave");
		userNames.put("mattyjay@tpg.com.au", "Matt");
		userNames.put("scott.gavens@gmail.com", "Scott");
		return userNames.get(email.toLowerCase());
	}
}
